﻿<%@ Page Title="结算查询" Language="C#" MasterPageFile="~/master/m.master" %>

<script runat="server"></script>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <link href="../assets/css/plugins/footable/footable.core.css" rel="stylesheet">
    <link href="../assets/css/shelf/base.css" rel="stylesheet">
    <link href="../assets/css/plugins/webuploader/webuploader.css" rel="stylesheet" />
    <link href="../assets/img/validate/validate.css" rel="stylesheet">
    <link href="../assets/css/plugins/iCheck/customsmall.css" rel="stylesheet" />
    <link href="../assets/css/plugins/bootstrap-datepicker/bootstrap-datepicker.min.css" rel="stylesheet" />
    <style type="text/css">
        .system-box .right-side {
            width: 100%;
        }

        .condition, .condition-title {
            border-bottom: 1px solid #dfdfdd;
        }

            .condition .form-control {
                padding: 2px 6px;
            }

        .module-info td .control-label {
            margin-right: 0px;
            width: 70px;
        }

        .printTitle {
            width: 100%;
            font-size: 18px;
            text-align: center;
        }

        .my-heder {
            width: 100%;
            margin-top: 20px;
            text-align: left;
        }

            .my-heder td {
                width: 33%;
            }

        .tab-sale-dtl {
            border-collapse: collapse;
            border-spacing: 0;
            border-left: 1px solid #888;
            border-top: 1px solid #888;
            background: #f5f5f5;
            width: 100%;
        }

            .tab-sale-dtl td {
                border-right: 1px solid #888;
                border-bottom: 1px solid #888;
                padding: 0px;
                /*width: 33%;*/
                text-align: center;
            }

        .print-modle {
            margin: 10px 0 0 -350px;
        }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="main" runat="Server">
    <div class="system-box">
        <div class="right-side">
            <div class="btns-container">
                <div class="modal inmodal print-modle" id="printWindow" tabindex="-1" role="dialog" aria-hidden="true">
                    <form id="checkModuleForm" method="get" action="#">
                        <div class="modal-dialog">
                            <div class="modal-content animated bounceInRight" style="width: 980px; height: 600px;">
                                <div class="modal-header">
                                    <span class="editTitle"></span>
                                    <button type="button" class="close" data-dismiss="modal">
                                        <span aria-hidden="true">&times;</span>
                                        <span class="sr-only">关闭</span>
                                    </button>
                                </div>
                                <div id="message" style="display: none"></div>
                                <div class="modal-body">
                                    <!--startprint-->
                                    <div class="printTitle">S&S月度结算单</div>
                                    <div style="width: 100%; text-align: center;">
                                        <table class="my-heder">
                                            <tr>
                                                <td>
                                                    <span>结算单号：</span>
                                                    <label id="BalanceCode"></label>
                                                </td>
                                                <td></td>
                                                <td>
                                                    <span>结算期间：</span>
                                                    <label id="BalanceDate"></label>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    <span>代&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码：</span>
                                                    <label id="ClientCode"></label>
                                                </td>
                                                <td><span>品&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;牌：</span>
                                                    <label id="CompanyID"></label>
                                                </td>
                                                <td>
                                                    <span>制单日期：</span>
                                                    <label id="CreateDate"></label>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    <span>名&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;称：</span>
                                                    <label id="RegistName"></label>
                                                </td>
                                                <td></td>
                                                <td>
                                                    <span>名&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;称：</span>
                                                    <label id="CompanyName"></label>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    <span>证&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;号：</span>
                                                    <label id="RegistCode"></label>
                                                </td>
                                                <td></td>
                                                <td>
                                                    <span>税&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;号：</span>
                                                    <label id="TaxNumber"></label>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    <span>地&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;址：</span>
                                                    <label id="RegistAddress"></label>
                                                </td>
                                                <td></td>
                                                <td>
                                                    <span>地&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;址：</span>
                                                    <label id="CompanyAddress"></label>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    <span>开&nbsp;&nbsp;户&nbsp;&nbsp;行：</span>
                                                    <label id="Client-Bank"></label>
                                                </td>
                                                <td></td>
                                                <td>
                                                    <span>开&nbsp;&nbsp;户&nbsp;&nbsp;行：</span>
                                                    <label id="Company-Bank"></label>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    <span>账&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;号：</span>
                                                    <label id="Client-AccountCode"></label>
                                                </td>
                                                <td></td>
                                                <td>
                                                    <span>账&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;号：</span>
                                                    <label id="Company-AccountCode"></label>
                                                </td>
                                            </tr>
                                        </table>
                                        <table class="my-heder">
                                            <tr>
                                                <td style="text-align: left;">销售与结算明细</td>
                                                <td></td>
                                                <td style="text-align: right;">金额：人员币元</td>
                                            </tr>
                                        </table>
                                        <table class="tab-sale-dtl">
                                            <thead>
                                            </thead>
                                            <tbody id="Data1">
                                            </tbody>
                                        </table>
                                        <table class="my-heder">
                                            <tr>
                                                <td style="text-align: left;">本期结算汇总：</td>
                                            </tr>
                                        </table>
                                        <table class="tab-sale-dtl">
                                            <thead>
                                            </thead>
                                            <tbody id="Data2">
                                            </tbody>
                                        </table>
                                        <table class="my-heder" style="text-align: left;">
                                            <tr>
                                                <td>审核信息：</td>
                                                <td></td>
                                            </tr>
                                            <tr>
                                                <td>销售审核：<label id="CheckName"></label></td>
                                                <td>财务审核：<label id="UserName"></label></td>
                                            </tr>
                                            <tr style="height: 80px;">
                                                <td>被特许方（盖章）：</td>
                                                <td>特许方（盖章）：</td>
                                            </tr>
                                        </table>
                                    </div>
                                    <!--endprint-->
                                </div>
                                <div class="modal-footer">
                                    <button type="button" id="BtnPrint" class="btn btn-primary">打印</button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
            <div class="condition">
                <label>结算状态：</label>
                <select class="form-control input-w110" id="search-SateID">
                    <option value="-1">全部</option>
                    <option value="0">未结算</option>
                    <option value="1">已结算</option>
                </select>
                <label>品牌：</label>
                <select class="form-control input-w110" id="search-CompanyID">
                   <%-- <option value="KM">KM</option>--%>
                </select>
                <label>年月：</label>
                <input name="search-date" placeholder="年月" class="form-control layer-date  input-w110" id="search-date" />
                <label>店铺：</label>
                <input class="form-control input-w155" id="search-Name" placeholder="输入公司名称或简称" />
                <div class="page-tips">
                    <i class="fa fa-lightbulb-o"></i>
                    <span>提示：结算店铺的数据按合同有效期间过虑。</span>
                </div>
            </div>
            <div class="right-content-box">
                <div class="ibox-content">
                    <table class="footable table table-stripped table-list loadTable">
                        <thead></thead>
                        <tbody id="pageList"></tbody>
                    </table>
                </div>
            </div>
            <div class="pageControl"></div>
        </div>
    </div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="footer" runat="Server">
    <script src="../assets/img/validate/learunui-validator.js"></script>
    <script src="../assets/js/page/pageControl.js"></script>
    <script src="../assets/js/page/rScroll.js"></script>
    <script src="../assets/js/page/tableControl.js"></script>
    <script src="../assets/js/plugins/webuploader/webuploader.min.js"></script>
    <script src="../assets/js/framework.js"></script>
    <script src="../assets/js/plugins/bootstrap-typeahead/bootstrap-typeahead.js"></script>
    <script src="../assets/js/plugins/linq/jquery.linq.min.js"></script>
    <script src="../assets/js/plugins/bootstrap-datepicker/bootstrap-datepicker.min.js"></script>
    <script src="../assets/js/plugins/iCheck/icheck.min.js"></script>
    <script src="../assets/js/plugins/iCheck/icheckAll.js"></script>
    <script>

        $('document').ready(function () {
            //检测是否已登录
            if (Y.checkNull(window.sessionStorage.getItem('login-info'))) {
                window.location.href = 'login.aspx';
            }
            else {
                var data = sessionStorage.getItem('login-info');
                var d = $.parseJSON(data);
                $("#UserName").text(d.NickName);
            }
            //模块ID
            var ModuleID = Y.getUrlParam("ModuleID");
            //绑定品牌
            commonUtil.bindDict({ DictValue: 'Brand' }, ['#search-CompanyID']);
            //当前页数
            var page = 1;
            commonUtil.loadBtnList(ModuleID, function () {
                $('#editBtn').attr("disabled", "disabled");
                rScroll();
            });

            $('#search-date').datepicker({
                format: "yyyy-mm",
                startView: 1,
                minViewMode: 1,
                language: "zh-CN",
                autoclose: true
            });

            var GetCurrData = function () {
                var data = new Date();
                var year = data.getFullYear();
                var month = data.getMonth();
                if (month < 10)
                    month = "0" + month;
                return year + "-" + month;
            }

            $("#search-date").val(GetCurrData());
            var _CompanyID = null;
            var _ShopID = null;
            var _SateID = null;
            //处理表头
            if ($('.footable thead').children().size() == 0) {
                tableControl.initThead('.footable',
                [
                    {
                        trClass: '',
                        children: [
                            //{ tdText: '公司代码', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' },
                            { tdText: '', c: 0, r: 0, tdHtml: '<span style="display:inline-block; width:46px;"><input id="checkAll" type="checkbox" /></span>', tdId: '', tdClass: 'diy-td-w46' },
                            { tdText: '结算单号', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' },
                            { tdText: '店铺名称', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' },
                            { tdText: '结算日期', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' },
                            { tdText: '销售收入', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' },
                            { tdText: '合同扣点', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' },
                            { tdText: '结算金额', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' },
                            { tdText: '结算状态', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' },
                        ]
                    }
                ]);
            }
            $(".footable").icheckAll({
                checkObj: ".checkItem",//表格下面的选择控件对象 选择器
                checkAllObj: "#checkAll",//全选 选择器
                clickoutBoxCheck: true, //点击行 不选中
                checkboxMargin: "5px 0 0 0 "//上右下左
            });

            var pageList = [];
            var loadList = function () {

                var SateID = $("#search-SateID").val();
                var CompanyID = $("#search-CompanyID").val();
                var BalanceDate = $("#search-date").val();
                var ShopStr = $("#search-Name").val();

                pageControl.init('Balance', 'GetBalaceList', {
                    SearchStr: $('#search-Name').val(),
                    Page: page,
                    Size: 100,
                    SateID: SateID,
                    BalanceDate: BalanceDate,
                    CompanyID: CompanyID,
                    ShopStr: ShopStr,
                    needLayer: true,
                    alwaysCallBack: function () {
                        $('#pageList').empty();
                    }
                }, function (result) {
                    pageList = [];
                    page = result.page;
                    var tBody = [];
                    if (!Y.checkNull(result.data)) {
                        if (result.data.Items.length > 0) {
                            result.data.Items.map(function (item, i) {
                                pageList.push(item);

                                var tempTr = {
                                    trClass: (i % 2 == 0 ? 'gradeX footable-even' : 'gradeX footable-odd'),
                                    children: [
                                        { tdText: '', c: 0, r: 0, tdHtml: '<span style="display:inline-block; width:46px;"><input data-id="' + (item.ShopID || ' ') + '" data-sate="' + (item.BalaceSate || ' ') + '" class="checkItem" type="checkbox" /></span>', tdId: '', tdClass: 'diy-td-w46' },
                                        { tdText: (item.ShopID || ''), c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'hide ShopID' },
                                        { tdText: (item.BalanceCode || ''), c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'BalanceCode' },
                                        { tdText: (item.ShopName || ''), c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'ShopName' },
                                        { tdText: (item.BalanceDate || ''), c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'BalanceDate' },
                                        { tdText: (item.RealAmount || ''), c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'RealAmount' },
                                        { tdText: (item.Discount || ''), c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'Discount' },
                                        { tdText: (item.BalanceAmount || ''), c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'BalanceAmount' },
                                        { tdText: '', c: 0, r: 0, tdHtml: '<span data-fin="' + item.BalaceSate + '" class="BalaceSate fin label label-' + (item.BalaceSate == 0 ? 'success' : 'primary') + ' ">' + (item.BalaceSate == 0 ? '未结算' : '已结算') + '</span>', tdId: '', tdClass: '' },
                                    ]
                                };
                                tBody.push(tempTr);
                            });
                        }
                        rScroll();
                    }
                    tableControl.initTbody('.footable', (tBody.length > 0 ? tBody : null), 13, null);

                    $(".footable").icheckAll({
                        checkObj: ".checkItem",//表格下面的选择控件对象 选择器
                        checkAllObj: "#checkAll",//全选 选择器
                        clickoutBoxCheck: true, //点击行 不选中
                        checkboxMargin: "5px 0 0 0 "//上右下左
                    });

                    _CompanyID = "";
                    $('#editBtn').attr('disabled', 'disabled');
                    $('#deleteBtn').attr('disabled', 'disabled');
                });
            }
          

            //选中记录
            $('#pageList').on('click', 'tr', function () {
                _ShopID = $(this).find('.ShopID').text();
                _SateID = $(this).find('.BalaceSate').text();
            });

            //查询按钮
            $('.btns-container').on('click', '#btn-search', function () {
                page = 1;
                loadList();
            });
            //结算按钮
            $('.btns-container').on('click', '#btn-balance', function () {
                var _count = $('#pageList .checkItem:checked').size();
                if (_count == 0) {
                    swal('至少要选择一项');
                    return false;
                }
                var shopList = "";
                var _flag = 0;
                $('#pageList .checkItem:checked').each(function (index) {
                    var sateId = eval($(this).attr('data-sate'));
                    if (sateId) {
                        _flag++;
                    }
                    shopList += (index > 0 ? "," : "") + $(this).attr('data-id') + "";
                })
                if (_flag > 0) {
                    swal('不要重复结算！');
                } else {
                    balanceData(shopList, true);
                }
            });
            //结算按钮
            $('.btns-container').on('click', '#btn-Isbalance', function () {
                var _count = $('#pageList .checkItem:checked').size();
                if (_count == 0) {
                    swal('至少要选择一项');
                    return false;
                }
                var shopList = "";
                var _flag = 0;
                $('#pageList .checkItem:checked').each(function (index) {
                    //var sateId = eval($(this).attr('data-sate'));
                    shopList += (index > 0 ? "," : "") + $(this).attr('data-id') + "";
                })
                balanceData(shopList,false);
            });
            var balanceData = function (ShopList,isBalance) {
                layer.msg('请稍等...', { icon: 16, shade: 0.5, time: 0, maxWidth: 200 });
                Y.API2('Balance', 'Baleance', {
                    CompanyID: $("#search-CompanyID").val(),
                    BalanceDate: $("#search-date").val(),
                    ShopList: ShopList,
                    IsBalance: isBalance
                }, function (data) {
                    layer.closeAll('dialog');
                    if (data.Success == false) {
                        swal(data.Message, '', 'error');
                    } else {
                        swal('结算成功！', '', 'success');
                        loadList();
                    }
                })
            }

            var loadCompany = function (_CompanyID) {
                Y.API2('CsrdCompany', 'GetCompany', {
                    CompanyID: _CompanyID
                }, function (data) {
                    if (data.Success == false) {
                        swal(data.Message, '', 'error');
                    } else {
                        $("#CompanyName").text(data.CompanyName);
                        $("#TaxNumber").text(data.TaxNumber);
                        $("#CompanyAddress").text(data.CompanyAddress);
                        $("#Company-Bank").text(data.BankName);
                        $("#Company-AccountCode").text(data.AccountCode);
                    }
                })
            }
            var loadClient = function (_ShopID) {
                Y.API2('Balance', 'GetClient', {
                    ShopID: _ShopID
                }, function (data) {
                    if (data.Success == false) {
                        swal(data.Message, '', 'error');
                    } else {
                        //console.log(data);
                        $("#ClientCode").text(data.ClientCode);
                        $("#RegistName").text(data.RegistName);
                        $("#RegistCode").text(data.RegistCode);
                        $("#RegistAddress").text(data.RedistAddress);
                        $("#Client-Bank").text(data.BankName);
                        $("#Client-AccountCode").text(data.AccountCode);
                        $("#CheckName").text(data.ShopLinkMan);
                    }
                })
            }
            var loadBalanceData = function (_CompanyID, _ShopID) {
                Y.API2('Balance', 'GetBalaceDtl', {
                    CompanyID: _CompanyID,
                    ShopID: _ShopID
                }, function (data) {
                    if (data.Success == false) {
                        swal(data.Message, '', 'error');
                    } else {
                        console.log(data);
                        $("#BalanceCode").text(data.BalanceCode);
                        $("#BalanceDate").text(data.BalanceDate);
                        $("#CreateDate").text(data.CreateDate);
                        var _html = '<tr><td>发票金额</td><td>不含税</td><td>税率</td><td>税额</td><td>应付金额</td></tr><tr>'
                                + '      <td>A</td>'
                                + '      <td>' + data.Discount + '</td>'
                                + '      <td>' + data.Qty + '</td>'
                                + '      <td>' + data.RealAmount + '</td>'
                                + '      <td>' + data.BalanceAmount + '</td>'
                                 + '  </tr>';
                        $("#Data1").html(_html);

                        var _html = '<tr><td>类别</td><td>结算扣率</td><td>销售数量</td><td>销售金额</td><td>结算金额</td></tr><tr>'
                                + '      <td>' + data.BalanceAmount + '</td>'
                                + '      <td>' + data.TaxFreeAmount + '</td>'
                                + '      <td>' + data.TaxRate + '</td>'
                                + '      <td>' + data.TaxAmount + '</td>'
                                + '      <td>' + data.BalanceAmount + '</td>'
                                 + '  </tr>';
                        $("#Data2").html(_html);
                    }
                })
            }
            //打印预览按钮
            $('.btns-container').on('click', '#btnPrint', function () {
                var _count = $('#pageList .checkItem:checked').size();
                if (_count == 0) {
                    swal('至少要选择一项');
                    return false;
                }
                if (_count > 1) {
                    swal('请选择一项操作！');
                    return false;
                }
                //if (_SateID == "未结算") {
                //    swal('未结算，无法打印预览！');
                //    return false;
                //}
                var _CompanyID = $("#search-CompanyID").val()
                var _BalanceDate = $("#search-date").val();

                var printBalance = $('#printBalance span', window.parent.document);
                printBalance.parent().attr('href', 'Csrd/BalanceDetail.aspx?CompanyID=' + _CompanyID + '&ShopID=' + _ShopID + '&BalanceDate=' + _BalanceDate);
                printBalance.click();

                //$('.editTitle').text('打印预览');
                //$("#CompanyID").text(_CompanyID);
                //loadCompany(_CompanyID);
                //loadClient(_ShopID);
                //loadBalanceData(_CompanyID, _ShopID);
            });

            var preview = function () {
                bdhtml = window.document.body.innerHTML;
                sprnstr = "<!--startprint-->";
                eprnstr = "<!--endprint-->";
                prnhtml = bdhtml.substr(bdhtml.indexOf(sprnstr) + 17);
                prnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr));
                window.document.body.innerHTML = prnhtml;
                window.print();
                window.document.body.innerHTML = bdhtml;
            }
            $('#BtnPrint').click(function () {
                //preview();
            });

          
        });

    </script>
</asp:Content>

